import copy

import cv2
bg_img = cv2.imread('1.jpg')
bg_img_copy = copy.copy(bg_img)
qi=(-1,-1)
zong=(-1,-1)

def on_mouse(event, x, y, flags, param):
    global qi,zong,bg_img,bg_img_copy
    if event == cv2.EVENT_LBUTTONDOWN:
        print(x, y,'anxia')
        qi=(x,y)
    if event == cv2.EVENT_MOUSEMOVE:
        print(x, y, "tanqi")
        zong=(x,y)
        if qi[0]>0 and qi[1]>0 :
            bg_img= copy.copy(bg_img_copy)
            cv2.line(bg_img, qi, zong, (0, 255, 255), 2)
            cv2.imshow('image', bg_img)
    if event == cv2.EVENT_LBUTTONUP:
        qi=(-1,-1)
        bg_img_copy = copy.copy(bg_img)
        # print(x, y,"tanqi")
        # zong=(x,y)
        # cv2.line(img, qi, zong, (0, 255, 255), 2)
        # cv2.imshow('image', img)

if __name__ == '__main__':
    cv2.namedWindow('image', cv2.WINDOW_NORMAL)
    cv2.resizeWindow('image', 640, 480)
    cv2.setMouseCallback('image', on_mouse)
    cv2.imshow('image', bg_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()